package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Excepton.PermissionException;
import com.itheima.PermissionService;
import com.itheima.dao.PermissionMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        Page page = PageHelper.startPage(currentPage, pageSize);
        List<Permission> permissionList = permissionMapper.pageQuery(queryString);
        return new PageResult(page.getTotal(),permissionList);
    }

    @Override
    public void addPermission(Permission permission) {
        permissionMapper.addPermission(permission);

    }

    @Override
    public void deletePermission(Integer id)throws PermissionException {

        Integer count = permissionMapper.queryPermissionAndRole(id);
        if (count != null && count > 0) {
             throw new PermissionException();
        }

        permissionMapper.deletePermission(id);

    }

    @Override
    public Permission queryPermissionById(Integer id) {

        return permissionMapper.queryPermissionById(id);
    }

    @Override
    public void updatePermission(Permission permission) {
        permissionMapper.updatePermission(permission);
    }
}
